package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    public static String drive;
    public static String url;
    public static String username;
    public static String password;
    public Connection conn=null;
    public static PreparedStatement ps=null;
    public static ResultSet rs=null;


    static {
        init();
    }

    public static void init() {
        Properties p = new Properties();
        InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            p.load(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        drive = p.getProperty("driver");
        url = p.getProperty("url");
        username = p.getProperty("username");
        password = p.getProperty("password");
    }

    public void getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(drive);
        conn = DriverManager.getConnection(url, username, password);
    }

    /**
     * 关闭数据库连接
     *
     * @param conn
     * @param ps
     * @param rs
     */
    public void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行查询语句
     *
     * @param sql
     * @param params
     * @return ResultSet
     * @illustrate 根据params参数的个数，动态设置占位符的值
     */
    public ResultSet executeQuerySql(String sql, Object... params) throws SQLException, ClassNotFoundException {
        getConnection();
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 执行增删改sql语句
     * @param sql
     * @param params
     * @return 执行结果
     * @illustrate 根据params参数的个数，动态设置占位符的值
     */
    public int executeUpdateSql(String sql, Object... params) throws SQLException, ClassNotFoundException {
        getConnection();
        int count = 0;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection(conn, ps, null);
        }
        return count;
    }

    public int executeSelectSql(String sql, Object... params) {
        int count = 0;
        try {
            rs = executeQuerySql(sql, params);
            if (rs.next()) {
                count = rs.getInt("count");
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeConnection(conn, ps, rs);
        }
        return count;
    }
}

